Thread: [sockets] Finding out if a browser client disconnected

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    41

    [sockets] Finding out if a browser client disconnected

    Hello!
    This is currently my biggest problem with the chatserver. I need to find out if a client is still connected so I can free up the slot and close the connection when a client leaves.
    Every connected client goes into a loop, constantly writing new stuff to the socket (the browser) which is the chat stream. Unfortunatly the "write" function I use to do this never returns -1, even if the browser already closed the connection so I have no way to find ouy if it's still there and receiving.
    The code looks like this:

    Code:
    	while(connected) {
    		// check the buffer for new text to send
    		if (strlen(chatter[myid].buffer) > 0) {
    			sprintf(sendbuf, "%s", chatter[myid].buffer);
    			sprintf(chatter[myid].buffer, "");
    			if(write(sock, sendbuf, strlen(sendbuf)) == -1)
    				connected = 0; // never ever happens
    			} 
    		}
    		sleep(1);
    	}
    So out of couriousity I tried what happens when I read() from the browser just as a test if it's still there:

    Code:
    	while(connected) {
    		// read something from the browser, just as a test...
    		if(read(sock, NULL, 1) == -1)
    			fprintf(stderr, "party! we are disconnected\n");
    		else
    			fprintf(stderr, "still connected :/\n");
    
    		sleep(1);
    	}
    This shows me that it _could_ work, because what happens is, that the function stops there at the read, waiting for the browser to actually send 1 char. Of course it doesn't. But when I close the connection from the browser now, it returns -1 and detects that it's disconnected! It doesn't work when I try to read 0 chars.
    There _has_ to be a way to find out if the browser is still connected. :/ Please help cause I'm lost.
    Last edited by Spark; 05-28-2002 at 05:23 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can tell then the client is gone, because recv will return zero.

    Have you researched the select() function? If not, have a look in this document.

    Basically, you can put file descriptors into a special struct array, and select will tell you if there's any data to be read from them. If it says there is, and when you do the recv and get zero bytes, this means the client has disconnected.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Beej is your friend.

    Use 'select', and the FD_ macros to test sockets for read/write availability.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    You can tell then the client is gone, because recv will return zero.

    Have you researched the select() function? If not, have a look in this document.

    Basically, you can put file descriptors into a special struct array, and select will tell you if there's any data to be read from them. If it says there is, and when you do the recv and get zero bytes, this means the client has disconnected.
    Ah, beat me to it. That's what happens when you talk on the phone while typing a reply.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    41

    I just tried it with select and just wanted to write "IT WORKS" but then I figured that it didn't work because of select but now the write functions returns -1 properly and that's why it worked. I removed the select stuff again and it still worked. Now this is completely beyond me why it didn't work before but... it's great.
    Of course it only works when I actually send something to the socket but that's no problem... I can just send a "ping?" every few seconds if it should be neccessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Client Detect Disconnected Equip.
    By sergioms in forum C# Programming
    Replies: 2
    Last Post: 01-25-2009, 06:49 AM
  2. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 07:36 PM
  3. Client works on a LAN but don't send all the data
    By Niara in forum Networking/Device Communication
    Replies: 9
    Last Post: 01-04-2007, 04:44 PM
  4. Getting Default Browser and Running It
    By Driver in forum Windows Programming
    Replies: 4
    Last Post: 10-22-2004, 02:08 PM
  5. [SOCKETS] Solaris server in C, NT client in VB
    By Daveg27 in forum C Programming
    Replies: 3
    Last Post: 05-23-2002, 10:02 AM